package com.yanqu.road.server.logic.activity.condition.operamask;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.OperaMaskSumScoreArgs;
import com.yanqu.road.entity.activity.operamask.OperaMaskConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.server.manger.activity.operamask.OperaMaskMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;

public class OperaMaskSumScoreRankCondition extends BaseNewRankActivityCondition {

    public OperaMaskSumScoreRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        OperaMaskSumScoreArgs args = (OperaMaskSumScoreArgs) object;
        BigInteger score = BigInteger.valueOf(args.getScore());
        String param = args.getParam();

        if (score.compareTo(getValue()) > 0) {
            setValue(score);
            setParam(param);
            rankChange();
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.OperaMaskSumScoreRank.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.OperaMaskSumScoreRank.getValue(), handle);
    }

    @Override
    public boolean canChange(ActivityInfo curActivity) {
        OperaMaskConfig config = OperaMaskMgr.getConfig();
        if (config == null) {
            return false;
        }
        ActivityInfo info = config.getActivityInfo();
        if (info.getActivityId() != activityInfo.getActivityId()) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        if (now < info.getBeginTime() || now > info.getEndTime() + DateHelper.MINUTE_SECONDS * 4) {
            return false;
        }
        return true;
    }
}
